<?php

/**
 * This is the model class for table "mb_pages".
 *
 * The followings are the available columns in table 'mb_pages':
 * @property string $pageId
 * @property string $pageParent
 * @property string $pageType
 * @property integer $pageItemId
 * @property string $pageUrl
 * @property string $pageTitle
 * @property string $pageContentShort
 * @property string $pageContentFull
 * @property string $pageImages
 * @property integer $pageActive
 * @property string $pageAuthor
 * @property integer $pageSort
 * @property string $pageAction
 * @property integer $pageSourceId
 */
class PageMaksud extends CActiveRecord
{

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{pages}}';
	}

	public function getDbConnection()
	{
		return Yii::app()->dbDest;
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array( 'pageUrl, pageTitle, pageAction', 'required' ),
			array( 'pageUrl', 'unique' ),
			array( 'pageImages', 'file', 'allowEmpty' => true, 'types' => 'jpg,png,bmp,jpeg' ),
			array( 'pageActive, pageSort', 'numerical', 'integerOnly' => true ),
			array( 'pageParent, pageAuthor', 'length', 'max' => 11 ),
			array( 'pageType', 'length', 'max' => 8 ),
			array( 'pageUrl, pageTitle, pageImages, pageAction', 'length', 'max' => 255 ),
			array( 'pageContentShort, pageContentFull', 'safe' ),
			array( 'pageId, pageParent, pageType, pageUrl, pageTitle, pageContentShort, pageContentFull, pageImages, pageActive, pageAuthor, pageSort, pageAction, pageSourceId', 'safe', 'on' => 'search' ),
		);
	}

	public function getDropdownItems( $level = 0 )
	{
		$query = "SELECT * FROM {{tree}} ";
		$models = Yii::app()->db->createCommand( $query )->queryAll( true );
		foreach( $models as $model )
			$array[ $model[ 'pageId' ] ] = $model[ 'pageTitle' ];
		return $array;
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'pageId' => 'ID',
			'pageParent' => 'Родитель',
			'pageType' => 'Тип страницы',
			'pageUrl' => 'Ссылка страницы',
			'pageTitle' => 'Название',
			'pageContentShort' => 'Краткое описание',
			'pageContentFull' => 'Полное содержимое',
			'pageImages' => 'Изображения',
			'pageActive' => 'Включена ли страница',
			'pageAuthor' => 'Редактор',
			'pageSort' => 'Сортировка',
			'pageAction' => 'Модуль',
			'pageSource' => 'ID из старой БД',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 *
	 * Typical usecase:
	 * - Initialize the model fields with values from filter form.
	 * - Execute this method to get CActiveDataProvider instance which will filter
	 * models according to data in model fields.
	 * - Pass data provider to CGridView, CListView or any similar widget.
	 *
	 * @return CActiveDataProvider the data provider that can return the models
	 * based on the search/filter conditions.
	 */
	public function search()
	{
		// @todo Please modify the following code to remove attributes that should not be searched.

		$criteria = new CDbCriteria;

		$criteria->compare('"pageId"', $this->pageId, true);
		$criteria->compare('"pageParent"', $this->pageParent, true);
		$criteria->compare('"pageType"', $this->pageType, true);
		$criteria->compare('"pageUrl"', $this->pageUrl, true);
		$criteria->compare('"pageTitle"', $this->pageTitle, true);
		$criteria->compare('"pageContentShort"', $this->pageContentShort, true);
		$criteria->compare('"pageContentFull"', $this->pageContentFull, true);
		$criteria->compare('"pageImages"', $this->pageImages, true);
		$criteria->compare('"pageActive"', $this->pageActive);
		$criteria->compare('"pageAuthor"', $this->pageAuthor, true);
		$criteria->compare('"pageSort"', $this->pageSort);
		$criteria->compare('"pageAction"', $this->pageAction, true);
		$criteria->compare('"pageSourceId"', $this->pageSourceId);

		return new CActiveDataProvider( $this, array(
			'criteria' => $criteria,
				) );
	}

	/**
	 * Returns the static model of the specified AR class.
	 * Please note that you should have this exact method in all your CActiveRecord descendants!
	 * @param string $className active record class name.
	 * @return Page the static model class
	 */
	public static function model( $className = __CLASS__ )
	{
		return parent::model( $className );
	}

	public function beforeSave()
	{

		if( parent::beforeSave() )
		{
			$uploadedFile = CUploadedFile::getInstance( $this, 'pageImages' );
			if( $uploadedFile !== NULL )
			{
				if( !$this->isNewRecord )
				{
					if( file_exists( "uploads/images/pages/" . $this->pageImages ) && !empty( $this->pageImages ) )
					{
						unlink( "uploads/images/pages/" . $this->pageImages );
					}
				}
				$newFileName = getUrl::cleanString( $this->pageTitle ) . '.' . pathinfo( $uploadedFile->name, PATHINFO_EXTENSION );
				copy( $uploadedFile->tempName, 'uploads/images/pages/' . $newFileName );
				$this->pageImages = $newFileName;
			}
			return true;
		}
		return FALSE;
	}

	protected function afterDelete()
	{
		parent::afterDelete();
		if( file_exists( "uploads/images/pages/" . $this->pageImages ) && !empty( $this->pageImages ) )
			unlink( "uploads/images/pages/" . $this->pageImages );
	}

}
